﻿using FastORM.Applicaiton;
using FastORM.Enum;
using FastORM.Handle;
using FastUtil.Config;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;

namespace FastCore.Test.ORM
{
    [TestClass]
    public class TransitionOperate
    {
        [TestMethod]
        public void ExecuteTransition()
        {
            CommonDao dao = new CommonDao(DataBaseClass.MySql, AppConfigUtil.Configuration["ConnectionStrings:Base_ConnectionString"]);
            string ExceptionStr = string.Empty;
            dao.ExecuteTransition(delegate ()
            {
                //增
                Frame_User user = new Frame_User();
                user.UserGuid = "XXXXXXSSSSSSS";
                user.LoginID = "yanpeng";
                user.Mobile = "18800001111";
                user.Age = 18;
                user.DisplayName = "颜鹏";
                user.Birthday = DateTime.Now;
                user.Gender = 1;
                user.Introduction = "XXXXXXXXXXXXXXXXX";
                user.Salary = 5356;
                dao.Insert(user);
                throw new Exception("11111");
                user.UserGuid = "XXXXXX";
                user.DisplayName = "TestPaul";
                dao.Update(user, new string[] { "TestPaul" });
            }, out ExceptionStr);
        }
        [TestMethod]
        public void ExecuteTransitionPass()
        {
            CommonDao dao = new CommonDao(DataBaseClass.MySql, AppConfigUtil.Configuration["ConnectionStrings:Base_ConnectionString"]);
            string ExceptionStr = string.Empty;
            DBUserOne One = new DBUserOne();
            DBUserTwo Two = new DBUserTwo();
            dao.ExecuteTransition(delegate ()
            {
                One.Insert();
                Two.Insert();
                throw new Exception("11111");
            }, out ExceptionStr);
        }

        [TestMethod]
        public void ExecuteTransitionNest()
        {
            CommonDao dao = new CommonDao(DataBaseClass.MySql, AppConfigUtil.Configuration["ConnectionStrings:Base_ConnectionString"]);
            string ExceptionStr = string.Empty;
            DBUserOne One = new DBUserOne();
            DBUserTwo Two = new DBUserTwo();
            DBUserThree Three = new DBUserThree();
            dao.ExecuteTransition(delegate ()
            {
                One.Insert();
                Two.Insert();
                Three.Insert();
            }, out ExceptionStr);
        }

        [TestMethod]
        public void ExecuteTransitionGroupWithDaoAndRow()
        {
            CommonDao dao = new CommonDao(DataBaseClass.MySql, AppConfigUtil.Configuration["ConnectionStrings:Base_ConnectionString"]);
            string ExceptionStr = string.Empty;
            dao.ExecuteTransition(delegate ()
            {
                //增
                Frame_User user = new Frame_User();
                user.UserGuid = "DAOYanpengxxx";
                user.DisplayName = "DaoYP1994";
                dao.Insert(user);
                CommonRow row = new CommonRow("Frame_User", "UserGuid", "RowYanpengxxx", DataBaseClass.MySql, AppConfigUtil.Configuration["ConnectionStrings:Base_ConnectionString"]);
                row["DisplayName"] = "RowYP1994";
                row.Insert();
                throw new Exception("11111");
            }, out ExceptionStr);
        }

        [TestMethod]
        public void ExecuteTransitionHandleRollBack()
        {
            try
            {
                CommonDao dao = new CommonDao(DataBaseClass.MySql, AppConfigUtil.Configuration["ConnectionStrings:Base_ConnectionString"]);
                TransitionManage.StartTransitionByConnectString(DataBaseClass.MySql, AppConfigUtil.Configuration["ConnectionStrings:Base_ConnectionString"]);
                Frame_User user = new Frame_User();
                user.UserGuid = "DAOYanpengxxx";
                user.DisplayName = "DaoYP1994";
                dao.Insert(user);
                TransitionManage.DoCommit();
                throw new Exception("11111");
            }
            catch (Exception)
            {

                TransitionManage.DoRollBack();
            }
        }

        [TestMethod]
        public void ExecuteTransitionHandle()
        {
            CommonDao dao = new CommonDao(DataBaseClass.MySql, AppConfigUtil.Configuration["ConnectionStrings:Base_ConnectionString"]);
            TransitionManage.StartTransitionByConnectString(DataBaseClass.MySql, AppConfigUtil.Configuration["ConnectionStrings:Base_ConnectionString"]);
            Frame_User user = new Frame_User();
            user.UserGuid = "DAOYanpengxxx";
            user.DisplayName = "DaoYP1994";
            dao.Insert(user);
            TransitionManage.DoCommit();
        }

    }
}
